package order

import "fmt"

// 观察者接口
type Observer interface {
	Update(event *OrderEvent) // 更新方法
	ID() string
}

// InventoryObserver 库存观察者
type InventoryObserver struct {
}

func (io *InventoryObserver) ID() string {
	return "inventory_system"
}

func (io *InventoryObserver) Update(event *OrderEvent) {
	// 库存系统只需要接收创建和取消事件
	switch event.EventType {
	case OrderCreated:
		fmt.Printf("[%s] 扣减库存 - 订单: %s, 商品: %v\n", io.ID(), event.OrderId, event.Payload)
	case OrderCancelled:
		fmt.Printf("[%s] 恢复库存 - 订单: %s\n", io.ID(), event.OrderId)
	}
}

// LogisticObserver 物流观察者
type LogisticObserver struct {
}

func (lo *LogisticObserver) ID() string {
	return "logistic_system"
}

func (lo *LogisticObserver) Update(event *OrderEvent) {
	// 物流系统只需要接收创建事件
	switch event.EventType {
	case OrderPaid:
		fmt.Printf("[%s] 订单已支付,准备发货 - 订单: %s, 地址: %v\n", lo.ID(), event.OrderId, event.Payload)
	}
}

// NotificationObserver 通知系统
type NotificationObserver struct {
}

func (no *NotificationObserver) ID() string {
	return "notification_system"
}

func (no *NotificationObserver) Update(event *OrderEvent) {
	var message string
	switch event.EventType {
	case OrderCreated:
		message = "您的订单已创建，等待支付"
	case OrderPaid:
		message = "支付成功，我们将尽快发货"
	case OrderShipped:
		message = "您的订单已发货"
	case OrderDelivered:
		message = "订单已完成，感谢您的购买"
	case OrderCancelled:
		message = "您的订单已取消"
	}

	fmt.Printf("[%s] 发送通知 - 用户: %s, 内容: %s\n",
		no.ID(), event.Payload.(string), message)

}
